Connectivity Visualizations¶
Connectivity Plot¶

from simpl_eeg import connectivity, eeg_objects
import warnings
warnings.filterwarnings('ignore')
Note
Please include the line below in your IDE so that the changes would be simultaneously reflected when you make a change to the python scripts.**
%load_ext autoreload
%autoreload 2
Define parameters¶
A detailed description of all parameters can be found in the connectivity.animate_connectivity docstring:
help(connectivity.animate_connectivity)
Help on function animate_connectivity in module simpl_eeg.connectivity:
animate_connectivity(epoch, calc_type='correlation', steps=20, pair_list=[], threshold=0, show_sphere=True, colormap='RdBu_r', vmin=None, vmax=None, line_width=None, title=None, **kwargs)
Animate 2d EEG nodes on scalp with lines representing connectivity
Args:
epochs (mne.epochs.Epochs): Epoch to visualize
calc_type (str: Connectivity calculation type. Defaults to "correlation".
pair_list ([str], optional): List of node pairs. Defaults to [], which indicates all pairs.
threshold (int, optional): Unsigned connectivity threshold to display connection. Defaults to 0.
show_sphere (bool, optional): Whether to show the cartoon head or not. Defaults to True.
steps (int, optional): Number of frames to use in correlation caluclation. Defaults to 20.
colormap (str, optional): Colour scheme to use. Defaults to "RdBu_r"
vmin (int, optional): The minimum for the scale. Defaults to None.
vmin (int, optional): The maximum for the scale. Defaults to None.
line_width (int, optional): The line width for the connections. Defaults to None for non-static width.
title (str, optional): The title to display on the plot. Defaults to None for no title.
**kwargs (dict, optional): Optional arguments to pass to mne.viz.plot_sensors().
Returns:
matplotlib.animation.Animation: Animation of connectivity plot
# change values below to values of interest
experiment_path = "../../data/927" # path to the experiment folder.
nth_epoch = 0
# connectivity parameters
vmin = -1
vmax = 1
colormap = "RdBu_r"
calc_type = "correlation"
pair_list = [] # select from the PAIR_OPTIONS below or use a custom pair.
line_width = None
steps = 50
threshold = 0
show_sphere = True
PAIR_OPTIONS = {
"all_pairs": [],
"local_anterior": "Fp1-F7, Fp2-F8, F7-C3, F4-C4, C4-F8, F3-C3",
"local_posterior": "T5-C3, T5-O1, C3-P3, C4-P4, C4-T6, T6-O2",
"far_coherence": "Fp1-T5, Fp2-T6, F7-T5, F7-P3, F7-O1, T5-F3, F3-P3, F4-P4, P4-F8, F8-T6, F8-O2, F4-T6",
"prefrontal_to_frontal_and_central": "Fp1-F3, Fp1-C3, Fp2-F4, Fp2-C4",
"occipital_to_parietal_and_central": "C3-O1, P3-O1, C4-O2, P4-O4",
"prefrontal_to_parietal": "Fp1-P3, Fp2-P4",
"frontal_to_occipital": "F3-O1, P4-O2",
"prefrontal_to_occipital": "Fp1-O1, Fp2-O2"
}
Create epoched data¶
For additional options see Creating EEG Objects section.
epochs = eeg_objects.Epochs(experiment_path)
epoch = epochs.get_nth_epoch(0)
Reading /Users/sasha/mds/simpl_eeg_capstone/data/927/fixica.fdt
Not setting metadata
Not setting metadata
33 matching events found
Applying baseline correction (mode: mean)
0 projection items activated
Loading data for 33 events and 2049 original time points ...
0 bad epochs dropped
Create the connectivity plot¶
Generating the animation¶
%matplotlib inline
%%capture
anim = connectivity.animate_connectivity(
epoch,
calc_type=calc_type,
steps=steps,
pair_list=pair_list,
threshold=threshold,
show_sphere=show_sphere,
colormap=colormap,
vmin=vmin,
vmax=vmax,
line_width=line_width,
)
from IPython.display import HTML
html_plot = anim.to_jshtml()
video = HTML(html_plot)
video
Saving the animation¶
Save as html¶
html_file_path = "../../exports/examples/connectivity.html" # change the file path to where you would like to save the file
html_file = open(html_file_path, "w")
html_file.write(html_plot)
html_file.close()
Save as gif¶
%%capture
anim = connectivity.animate_connectivity(epoch, vmin=-1, vmax=1, pair_list=PAIR_OPTIONS["far_coherence"])
gif_file_path = "../../exports/examples/connectivity.gif" # change the file path to where you would like to save the file
anim.save(gif_file_path, fps=3, dpi=300) # set frames per second (fps) and resolution (dpi)
Save as mp4¶
mp4_file_path = "../../exports/examples/connectivity.mp4"
anim.save(mp4_file_path, fps=3, dpi=300)
Note
If FFMpegWriter does not work on your computer you can save the file as a gif first and then convert it into mp4 file by running the code below.
import moviepy.editor as mp
clip = mp.VideoFileClip(gif_file_path) # change the file path to where you saved the gif file
clip.write_videofile(mp4_file_path) # change the file path to where you would like to save the mp4 file
Connectivity Circle Plot¶

Define parameters¶
A detailed description of all parameters can be found in the connectivity.animate_connectivity_circle docstring:
help(connectivity.animate_connectivity_circle)
Help on function animate_connectivity_circle in module simpl_eeg.connectivity:
animate_connectivity_circle(epoch, calc_type='correlation', max_connections=50, steps=20, colormap='RdBu_r', vmin=None, vmax=None, line_width=None, title=None, **kwargs)
Animate connectivity circle
Args:
epoch (mne.epochs.Epochs): Epoch to visualize
calc_type (str, optional): Connectivity calculation type. Defaults to "correlation".
max_connections (int, optional): Number of connections to display. Defaults to 50.
steps (int, optional): Number of frames to use in correlation caluclation. Defaults to 20.
colormap (str, optional): Colour scheme to use. Defaults to "RdBu_r".
vmin (int, optional): The minimum for the scale. Defaults to None.
vmin (int, optional): The maximum for the scale. Defaults to None.
line_width (int, optional): The line width for the connections. Defaults to None for non-static width.
title (str, optional): The title to display on the plot. Defaults to None for no title.
**kwargs (dict, optional): Optional arguments to pass to mne.viz.plot_connectivity_circle().
Returns:
matplotlib.animation.Animation: Animation of connectivity plot
# change values below to values of interest
# connectivity circle parameters
vmin = -1
vmax = 1
colormap = "RdBu_r"
calc_type = "correlation"
line_width = 1
steps = 50
max_connections = 50
Generating the animation¶
%matplotlib notebook
%%capture
anim = connectivity.animate_connectivity_circle(
epoch,
calc_type=calc_type,
max_connections=max_connections,
steps=steps,
colormap=colormap,
vmin=vmin,
vmax=vmax,
line_width=line_width,
)
from IPython.display import HTML
html_plot = anim.to_jshtml()
video = HTML(html_plot)
video
Saving the animation¶
Save as html¶
html_file_path = "../../exports/examples/connectivity_circle.html" # change the file path to where you would like to save the file
html_file = open(html_file_path, "w")
html_file.write(html_plot)
html_file.close()
Save as gif¶
%%capture
anim = connectivity.animate_connectivity_circle(epoch)
gif_file_path = "../../exports/examples/connectivity_circle.gif" # change the file path to where you would like to save the file
anim.save(gif_file_path, fps=3, dpi=300)
Save as mp4¶
%%capture
mp4_file_path = "../../exports/examples/connectivity_cicle.mp4"
anim.save(mp4_file_path, fps=3, dpi=300)
Note
If FFMpegWriter does not work on your computer you can save the file as a gif first and then convert it into mp4 file by running the code below.
import moviepy.editor as mp
clip = mp.VideoFileClip(gif_file_path) # change the file path to where you saved the gif file
clip.write_videofile(mp4_file_path) # change the file path to where you would like to save the mp4 file